<?php
class ModelCatalogAttribute extends Model {

	/**
	 * Add new attribute.
	 */
	public function addAttribute($data) {
	
		$this->db->query("INSERT INTO " . DB_PREFIX . "attribute SET name = '" . $this->db->escape($data['name']) . "'");
	
		$attribute_id = $this->db->getLastId();
		
		// Default value
		if (isset($data['default'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "attribute SET default_value = '" . $this->db->escape($data['default']) . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
		}
		
		// Data type
		if (isset($data['data_type'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "attribute SET data_type = '" . $this->db->escape($data['data_type']) . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
		}
		
		// Description
		if (isset($data['description'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "attribute SET description = '" . $this->db->escape($data['description']) . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
		}

		$this->cache->delete('attribute');
		
		// Return the id of the inserted attribute
		return $attribute_id;
	}
	
	/**
	 * Edit attribute
	 */
	public function editAttribute($attribute_id, $data) {
		$this->db->query("UPDATE " . DB_PREFIX . "attribute SET name = '" . $this->db->escape($data['name']) . "' WHERE attribute_id = '" . (int)$attribute_id . "'");

		// Default value
		if (isset($data['default'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "attribute SET default_value = '" . $this->db->escape($data['default']) . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
		}

		// Data type
		if (isset($data['data_type'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "attribute SET data_type = '" . $this->db->escape($data['data_type']) . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
		}
		
		// Description
		if (isset($data['description'])) {
			$this->db->query("UPDATE " . DB_PREFIX . "attribute SET description = '" . $this->db->escape($data['description']) . "' WHERE attribute_id = '" . (int)$attribute_id . "'");
		}
		
		$this->cache->delete('attribute');
	}
	
	/**
	 * Delete all information related to attributes.
	 */
	public function deleteAttribute($attribute_id) {
		$this->db->query("DELETE FROM " . DB_PREFIX . "attribute WHERE attribute_id = '" . (int)$attribute_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "category_attribute WHERE attribute_id = '" . (int)$attribute_id . "'");
		$this->db->query("DELETE FROM " . DB_PREFIX . "product_attribute_value WHERE attribute_id = '" . (int)$attribute_id . "'");
				
		$this->cache->delete('attribute');
	} 

	/**
	 * Get a particular attribute.
	 */ 
	public function getAttribute($attribute_id) {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "attribute WHERE attribute_id = '" . (int)$attribute_id . "'");
		
		return $query->row;
	} 	
	
	/**
	 * Get all attributes.
	 */ 
	public function getAttributes() {
		$query = $this->db->query("SELECT DISTINCT * FROM " . DB_PREFIX . "attribute");
		
		return $query->rows;
	}
	
	/**
	 * Get total attributes.
	 */
	public function getTotalAttributes() {
		$query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "attribute");
		
		return $query->row['total'];
	}
	
	public function getAllAttributes($data = array()) {
		$query = $this->db->query("SELECT * FROM " . DB_PREFIX . "attribute");
		
		return $query->rows;
	}
	

}
?>